package com.qf.dao.impl;

import com.qf.dao.BookDao;
import com.qf.entity.Book;
import com.qf.entity.Page;
import com.qf.util.DBUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.VariableLengthInstruction;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.management.Query;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 孙亚梅
 * @version V1.0
 * @Project bookApp-homework
 * @Package com.qf.dao.impl
 * @Description:
 * @Date 2022/5/14 20:12
 */
public class BookDaoImpl implements BookDao {
   private QueryRunner runner= new QueryRunner(DBUtil.getDateSource());

    @Override
    public List<Book> selectAll() {
        String sql="select * from t_book";
        try {
            return runner.query(sql, new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Book> selectAllByPage(Page page) {
      String sql="select *from t_book limit ?,?;";
        try {
            List<Book> list = runner.query(sql, new BeanListHandler<Book>(Book.class), page.getStartRows(), page.getPageSize());
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectAllByPageCount() {
        String sql ="select count(*) from t_book;";
        try {
            long count = runner.query(sql, new ScalarHandler<Long>());
            return  (int)count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addOne(Book book) {
        String sql ="insert into t_book values(null,?,?,?,?,?)";
        try {
            return  runner.update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getPublisher(),book.getDescription());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOne(String bookId) {
        String sql ="delete from t_book where id=?";
        try {
            return  runner.update(sql,bookId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Book selectOne(String bookId) {
        String sql = "select * from t_book where id = ? ";
        try {
            return runner.query(sql, new BeanHandler<>(Book.class), bookId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateOne(Book book) {
        String sql = "update t_book set name = ?, price=? ,author=?, publisher=?, description=? where id = ? ";
        try {
            return runner.update(sql, book.getName(),book.getPrice(),book.getAuthor(),book.getPublisher(),book.getDescription(),book.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
